﻿using Coolzon.UI.Core.Parts;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media.Animation;
using RT = Coolzon.Metadata.Runtime;

namespace Coolzon.UI.Core.Runtime.Script
{
    /// <summary>
    /// 用于供脚本访问组件的类
    /// </summary>
    public class PartScriptObject
    {
        private WeakReference _part;
        public PartScriptObject(Widget part)
        {
            _part = new WeakReference(part);
        }

        protected T GetTarget<T>() where T : class
        {
            return _part.Target as T;
        }

        /// <summary>
        /// 设置组件的属性值
        /// </summary>
        /// <param name="propertyName"></param>
        /// <param name="value"></param>
        public void setPropertyXmlString(string propertyName, object value)
        {
            Widget part = _part.Target as Widget;
            if (part != null)
            {
                XProperty.XPropertyDescription description = XPropertyUtility.GetXPropertyDescription(part, propertyName);
                if (description != null)
                {
                    ConvertContext context = new ConvertContext();
                    RT.ValueNode valueNode = RT.ValueNode.Parse(Convert.ToString(value));
                    XPropertyUtility.SetXPropertyValue(context, part, description, new RT.Property()
                    {
                        Name = propertyName,
                        ValueNode = valueNode
                    });
                }
            }
        }

        /// <summary>
        /// 获取组件的属性值
        /// </summary>
        /// <param name="propertyName"></param>
        /// <returns></returns>
        public object getPropertyXmlString(string propertyName)
        {
            Widget part = _part.Target as Widget;
            if (part != null)
            {
                XProperty.XPropertyDescription description = XPropertyUtility.GetXPropertyDescription(part, propertyName);
                if (description != null)
                {
                    ConvertContext context = new ConvertContext();
                    return XPropertyUtility.GetXPropertyValue(context, part, description);
                }
            }
            return null;
        }

        /// <summary>
        /// 运行动画
        /// </summary>
        /// <param name="animation"></param>
        /// <returns></returns>
        public void runAnimation(object animation)
        {
            Coolzon.UI.Core.Runtime.Script.Animation.AnimationScriptObject animationObj = (Coolzon.UI.Core.Runtime.Script.Animation.AnimationScriptObject)animation;
            //System.Windows.Media.Animation.Storyboard storyboard = new System.Windows.Media.Animation.Storyboard();
            Storyboard storyboard = new Storyboard();
            storyboard.FillBehavior = System.Windows.Media.Animation.FillBehavior.HoldEnd;
            double duration = 0;
            animationObj.AppendToStoryboard((this._part.Target as Widget), ref duration, storyboard);
            storyboard.Duration = new System.Windows.Duration(TimeSpan.FromSeconds(duration));
            (this._part.Target as Widget).RunAnimation(storyboard);
        }
    }
}
